package april.test_4_1;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {
    public String reverses(String original_str) {
        // write code here
        StringBuilder sb = new StringBuilder();
        String[] words = original_str.split(" ");
        for (String word : words) {
            sb.append(reverseString(word));
            sb.append(" ");
        }
        return sb.toString().trim();
    }
    public String reverseString(String word) {
        StringBuilder sb = new StringBuilder();
        for (char c : word.toCharArray()) {
            if(Character.isLowerCase(c)) {
                sb.append(c);
            }
        }
        sb.reverse();
        char[] chars = word.toCharArray();
        int index = 0;
        for (int i = 0; i < chars.length; i++) {
            if(Character.isLowerCase(chars[i])) {
                chars[i] = sb.charAt(index++);
            }
        }
        return new String(chars);
    }
    public int[] solution(int[] costs, int coins) {
        // write code her
        int[] arr = new int[costs.length];
        for(int i = 0; i < costs.length; i++) {
            arr[i] = costs[i];
        }
        Arrays.sort(arr);
        int tmp = coins;
        int[] results = new int[costs.length];
        for (int i = 0; i < arr.length; i++) {
            tmp -= arr[i];
            if(tmp >= 0) {
                for (int j = 0; j < costs.length; j++) {
                    if(costs[j] != -1 && costs[j] == arr[i]) {
                        results[j] = costs[j];
                        costs[j] = -1;
                        break;
                    }
                }
            } else {
                break;
            }
        }
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < costs.length; i++) {
            if(costs[i] > 0) {
                list.add(results[i]);
            }
        }
        int[] count = new int[list.size()];
        for (int  i = 0; i < list.size(); i++) {
            count[i] = list.get(i);
        }
        return count;
    }
    public ListNode Rotate(ListNode head, int k) {
        // write code here
        if(head == null || head.next == null ||k == 0) {
            return head;
        }
        int len = 1;
        ListNode cur = head;
        while (cur.next != null) {
            len ++;
            cur = cur.next;
        }
        int count = len = k % len;
        if(count == len) {
            return head;
        }

        cur.next = head;
        while (count > 0) {
            cur = cur.next;
            count--;
        }
        ListNode newHead = cur.next;
        cur.next = null;
        return newHead;
    }
}
